package com.example.springbatch.writer;

import com.example.springbatch.entity.Person;
import org.springframework.batch.item.Chunk;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.List;

@Component
public class CustomJdbcBatchItemWriter implements ItemWriter<Person> {

    private final JdbcTemplate jdbcTemplate;

    public CustomJdbcBatchItemWriter(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void write(Chunk<? extends Person> chunk) throws Exception {
        List<? extends Person> persons = chunk.getItems();

        for (Person person : persons) {
            System.out.println("Writing person: " + person);
            /*String sql = "INSERT INTO persons (name, age) VALUES (?, ?)";
            jdbcTemplate.update(sql, person.getName(), person.getAge());*/
        }
    }

}